/*********************************************************************
	\file :           os_sw_layers\bios\services\scheduler\SchM.c
	\brief:           Periodic tasks.
	\author:          Manuel Sandoval
	\version:         1.0
	\date:            Sat, 01, March 2014
*********************************************************************/


/******************************* Includes**********************************/

#include "SchM.h"


/******************************* Defines **********************************/


/******************************* Enumerations******************************/


/******************************* Structures *******************************/


/******************************* Global Variables **********************************/

u8 gu8SchM_Counter;


SchM_TaskIDType SchM_Task_ID_Activated;
SchM_TaskIDType SchM_Task_ID_Running;
SchM_TaskIDType SchM_Task_ID_Backup;

SchM_TaskConfigType* ptrSchM_Task_Running;

//SchM_TaskConfigType* SchM_Tasks_Local[TASK_MAX_NUM];

/******************************* Prototypes ********************************/
extern void SchM_Task_Background( void );

/******************************* Functions ********************************/



/***************************************************************************
*
*   FUNCTION NAME: SchM_Init
*
***************************************************************************/
/**
* @brief Scheduler Initialization.
*
* This function allows the application to Initialize the Scheduler.
*
* @param[in].
*
* @return .
*
* @par Notification:
* None.
*
***************************************************************************/
void SchM_Init(SchM_TaskConfigType* SchM_Config)
{


	SchM_Task_ID_Activated = NONE;
	SchM_Task_ID_Running = NONE;
	SchM_Task_ID_Backup = NONE;
  ptrSchM_Task_Running = NONE;


}



/***************************************************************************
*
*   FUNCTION NAME: SchM_DeInit
*
***************************************************************************/
/**
* @brief Scheduler De-Initialization.
*
* This function allows the application to De-Initialize the Scheduler.
*
* @param[in].
*
* @return .
*
* @par Notification:
* None.
*
***************************************************************************/
void SchM_DeInit(void)
{


}


/***************************************************************************
*
*   FUNCTION NAME: SchM_Start
*
***************************************************************************/
/**
* @brief Scheduler start function.
*
*
* @param[in].
*
* @return .
*
* @par Notification:
* None.
*
***************************************************************************/
/** Scheduler start function */
void SchM_Start( void  )
{

	if(NULL != SchM_Tasks_Config[TASKS_1_MS].CallbackFunction)
	{
		/*!< Executing callback */
		SchM_Tasks_Config[TASKS_1_MS].CallbackFunction();

		SchM_Task_ID_Running = SchM_Tasks_Config[TASKS_1_MS].enTaskId;
	}

}



/***************************************************************************
*
*   FUNCTION NAME: SchM_OsTick
*
***************************************************************************/
/**
* @brief OS tick callback function.
*
* @param[in].
*
* @return .
*
* @par Notification:
* None.
*
***************************************************************************/

/** OS tick callback function - */
void SchM_OsTick(void)
{

	u8 u8Iterator;


	for(u8Iterator = 0; u8Iterator < TASK_MAX_NUM; u8Iterator++)
	{

		if( ( gu8SchM_Counter & SchM_Tasks_Config[u8Iterator].TaskMask ) == SchM_Tasks_Config[u8Iterator].TaskOffset )
			{

				if(NULL != SchM_Tasks_Config[u8Iterator].CallbackFunction)
				{

          SchM_Task_ID_Running = SchM_Tasks_Config[u8Iterator].enTaskId;

					/*!< Executing callback */
					SchM_Tasks_Config[u8Iterator].CallbackFunction();

					/*!< Break For */
					break;
				}
				else
				{
					/* do nothing */
				}
			}
			else
			{
        SchM_Task_ID_Running = NONE;
				SchM_Background();
			}
	}


  /*-- Update scheduler control variable --*/
	gu8SchM_Counter++;


}


/***************************************************************************
*
*   FUNCTION NAME: SchM_Background
*
***************************************************************************/
/**
* @brief Backgroud Task.
*
* @param[in].
*
* @return .
*
* @par Notification:
* None.
*
***************************************************************************/

/** Backgroud Task - */
void SchM_Background( void  )
{

  SchM_Task_Background();
	/* do nothing */

}



/** @} *//* End of Scheduler Group */


/*********************************************************************
	File	: os_sw_layers\bios\services\scheduler\SchM.c
*********************************************************************/
